<?php

class View_Helper {
    public static function makeUri ($string = '', $param = array()) {
        
        if (! is_array($param)) {
            $params = func_get_args();
            array_shift($params);
            $param = array();
            while (! empty($params)) {
                $param[array_shift($params)] = array_shift($params);
            }
        }
        
        return Uri::make($string, $param);
    }
    
    public static function makeTime ($timestamp, $format = 'Y-m-d H:i:s') {
        return date($format, $timestamp);
    }
    
    public static function getParam ($key) {
        return Context::getParam($key);
    }
    
    public static function getThumb ($path, $suffix = '_ico') {
        $info = pathinfo($path);
        return $info['dirname'] . '/' . $info['filename'] . $suffix . '.' . $info['extension'];
    }
    
    public static function import ($file) {
        $info = pathinfo($file);
        
        switch ($info['extension']) {
            case 'js':
                $uri = '<script type="text/javascript" src="/public/script/' . $file . '"></script>';
                break;
            case 'css':
                $uri = '<link href="/public/style/' . $file . '" rel="stylesheet" type="text/css">';
        }
        
        return $uri;
    }
    
    public static function img ($file) {
        return '/public/image/' . $file;
    }
    
    public static function formInput (array $attributes) {
        if ($attributes['label'] && $attributes['id']){
            $html = "<div><label for=\"{$attributes['id']}\" >{$attributes['label']}</label></div>";
        }
        $html .= "<div><input ";
        foreach ($attributes as $key => $attribute) {
            $html .= $key . "=\"{$attribute}\" ";
        }
        $html .= "/></div>";
        return $html;
    }
    
    public static function formText (array $attributes, $value = '') {
        if ($attributes['label'] && $attributes['id']){
            $html = "<div><label for=\"{$attributes['id']}\" >{$attributes['label']}</label></div>";
        }
        $html .= "<div><textarea ";
        foreach ($attributes as $key => $attribute) {
            $html .= $key . "=\"{$attribute}\" ";
        }
        $html .= ">{$value}</textarea></div>";
        return $html;
    }
    
    public static function formSelect (array $attributes, array $value, $seleted = '') {
        if ($attributes['label'] && $attributes['id']){        
            $html = "<div><label for=\"{$attributes['id']}\" >{$attributes['label']}</label></div>";
        }
        $html .= "<div><select ";
        foreach ($attributes as $key => $attribute) {
            $html .= $key . "=\"{$attribute}\" ";
        }
        $html .= ">";
        foreach ($value as $k => $v) {
            $html .= "<option";
            if ($seleted == $k) {
            	$html .= ' selected="selected"';
            }            
            $html .= " value=\"{$k}\">$v</option>";
        }
        $html .= "</select></div>";
        return $html;
    }
    
    public static function formCheckbox (array $attributes, array $value, array $checked = array()) {
        if ($attributes['label']){        
            $html = "<div><label>{$attributes['label']}</label></div>";
        }        
        $html .= '<div>';
        foreach ($value as $k => $v) {
            $html .= '<input type="checkbox" ';
            foreach ($attributes as $key => $attribute) {
                $html .= $key . "=\"{$attribute}\" ";
            }
            if (in_array($k, $checked)) {
            	$html .= ' checked="checked"';
            }   
            $html .= "value=\"{$k}\" ";
            $html .= "/>$v";
        }
        $html .= '</div>';
        return $html;
    }

}
?>